
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import datetime
from textparamsparse import *

class CoSailTextFile():
    def __init__(self, filename = None):
        self.filename = filename
        self.f = None

    def open(self, m):
        try:
            self.f = open(self.filename, m)
        except Exception as e:
            print(e)
            self.f = None
            return False
        return True

    def writeLine(self, line):
        try:
            self.f.write(line + "\n")
            return True
        except Exception as  e:
            print(e)
            return False

    def writeLines(self, lines):
        for line in lines:
            if self.writeLine(line) == False:
                return False
        return True

    def readLine(self):
        text = None
        try:
             text = self.f.readline()
        except Exception as e:
            print(e)
            return None

        return text

    def readAll(self):
        return self.f.read()

    def readLines(self):
        return self.f.readlines()


    def close(self):
        if self.f != None:
            try:
                self.f.close()
                self.f = None
            except Exception as e:
                print(e)
                return False
            return True
        return True

    def isOpen(self):
        return self.f != None

def getFilePathExtend(fileName):
    filePath, suffix = os.path.splitext(fileName)
    filePath = os.path.dirname(fileName)
    l = len(suffix)
    fileName = os.path.split(fileName)[1]
    return filePath,  suffix[1:l + 1], fileName


filesdict = {}
filesuffixdict = {}
def scansamenamefile(path):

    if os.path.exists(path) == False:
        print("Store Path :" + path + " isn't exist!")
        return None
    try:
        files = os.listdir(path)
    except FileNotFoundError:
        print("File Not FoundError")
        return None
    for fi in files:
        # file full path
        # sleep 1
        # time.sleep(1)
        fi_d = os.path.join(path, fi)
        samefilelist = None
        # is directory
        if os.path.isdir(fi_d):
            if os.path.islink(fi_d):
                continue
            """ recursion"""
            scansamenamefile(fi_d)

            # print("dir = ", fi_d)
        # is file
        else:
            file_path, suffix, file_name = getFilePathExtend(fi_d)


            tmpfullpath = file_path + "/" + file_name

            try:
                samefilelist = filesdict[file_name]
            except Exception as e:
                print(e)
                samefilelist = None
            if samefilelist == None:
                samefilelist = []
                filesdict[file_name] = samefilelist

            fileattrdict = {}

            fileattrdict["filepath"] = tmpfullpath
            fileattrdict["size"] = os.path.getsize(tmpfullpath)
            if suffix == None or suffix == "":
               fileattrdict["suffix"] = "unknow"
               filesuffixdict["unknow"] = "unknow"
            else:
                fileattrdict["suffix"] = suffix.lower()
                filesuffixdict[suffix.lower()] = suffix.lower()

            samefilelist.append(fileattrdict)
    return filesdict

def statallfiles(d):
    v = 0;
    o = None
    for k in d:
        o = d[k]
        v += len(o)
    return v

def statallfilesbytype(d, suffix="sgy"):
    v = 0;
    obj = None
    for k in d:
        obj = d[k]
        if len(obj) == 0:
            continue
        if obj[0]["suffix"] == suffix:
            v += len(obj)
    return v

def statsamefilesbytype(d, suffix = "sgy"):
    """

    :param type:
    :return:
    """
    v = 0
    av = 0
    o = None
    for k in d:
        o = d[k]
        l = len(o)
        if l <=1:
            continue
        if o[0]["suffix"] == suffix:
            v += 1
            av += l

    return v, av


def outduplicatefilemsgbytype(d, textFile, suffix = "sgy"):
    v = 0;
    o = None
    textFile.writeLine("")
    textFile.writeLine("**************Duplicate file list, suffix :"  + suffix)

    for k in d:
        o = d[k]
        if len(o) <= 1:
            continue
        if o[0]["suffix"] == suffix:
            textFile.writeLine("")
            txt = "file name : " + k + " Duplicate : " + str(len(o))
            textFile.writeLine(txt)

            txt = "file list :"
            for obj in o:
                txt = "fileputh :" + obj["filepath"] + " size : " + str(obj["size"])
                textFile.writeLine(txt)


def statsamenamefiles(d):
    v = 0
    av = 0
    o = None
    for k in d:
        o = d[k]
        if len(o) >=2:
            v += 1
            av += len(o)
    return v, av

def outduplicatefilemsg(d, textFile):
    v = 0;
    o = None
    textFile.writeLine("")
    textFile.writeLine("**************Duplicate file list*****************")

    for k in d:
        o = d[k]
        if len(o) >= 2:
            textFile.writeLine("")
            txt = "file name : " + k + " Duplicate : " +str(len(o))
            textFile.writeLine(txt)

            txt = "file list :"
            for obj in o:
                txt ="fileputh :" + obj["filepath"] + " size : " + str(obj["size"])
                textFile.writeLine(txt)

def outonefilemsg(d, textFile):
    v = 0;
    o = None
    textFile.writeLine("")
    textFile.writeLine("**************Unique file list*****************")

    for k in d:
        o = d[k]
        if len(o) == 1:

            for obj in o:
                txt ="fileputh :" + obj["filepath"] + " size : " + str(obj["size"])
                textFile.writeLine(txt)




def statpathfileattr(pathlistfilename):
    """
    :param pathlistfilename:  要统计的文件目录列表名，每行一个目录
    :return:
    """
    f = CoSailTextFile(pathlistfilename)
    f.open("r")
    lines = f.readLines()
    f.close()

    if lines == None or len(lines) == 0:
        print("params is null")
        return False

    paramsdict = parseparams(lines)
    if paramsdict == None or len(paramsdict) == 0:
        print("params is null")
        return False

    sourcepath = None

    try:
        sourcepath = paramsdict["sourcepath"]
    except Exception as e:
        sourcepath = None

    logpath = None

    try:
        logpath= paramsdict["logpath"]
    except Exception as e:
        logpath = None

    if sourcepath == None:
        print("source path list is null")
        return False
    if logpath == None:
        print("log path is null ")
        return False

    pathlist = None

    if isinstance(sourcepath, str):
        sourcepath = sourcepath.strip()
        if sourcepath[0] == "#":
            return False

        filesdict.clear()


        statpathfileattrbypath(sourcepath, logpath)
        return  True


    if isinstance(sourcepath, list):
        pathlist = sourcepath

    for line in pathlist:
        line = line.strip('\n')
        if line[0] == "#":
            continue

        filesdict.clear()

        print("filedict id = ", id(filesdict))

        statpathfileattrbypath(line, logpath)


def statpathfileattrbypath(path, logpath):
    d = scansamenamefile(path)

    if d == None :
        return

    print("allfiles", statallfiles(d))

    print("all file types : ", len(filesuffixdict))

    print("")

    print("all sgy files : ", statallfilesbytype(d, suffix="sgy"))

    print("all segy files : ", statallfilesbytype(d, suffix="segy"))
    print("")

    dfiles ,dfileall = statsamenamefiles(d)

    print("sameallfiles",dfiles, "samefilescounts : " ,dfileall)
    t = path.replace("/", "-");
    t = t.replace("\\", "-");
    t = t.replace(":", "")
    t = t[1:]
    lonfilename = logpath + "/" + t + datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S') + ".log"
    lonfilename = lonfilename.replace("//", "/")
    f = CoSailTextFile(lonfilename)

    f.open("w")

    f.writeLine("stat path : " + path)
    f.writeLine("all files : " + str(statallfiles(d)))

    f.writeLine("all file types  : " + str(len(filesuffixdict)))

    f.writeLine("all sgy files : " + str(statallfilesbytype(d, suffix="sgy")))
    f.writeLine("all segy files : " + str(statallfilesbytype(d, suffix="segy")))


    f.writeLine("Duplicate files : " + str(dfiles) + " All Duplicate file counts : " + str(dfileall) )

    dsgyfiles, dsgyfileall = statsamefilesbytype(d, suffix="sgy")

    f.writeLine("Duplicate sgy files : " + str(dsgyfiles) + " All Duplicate file counts : " + str(dsgyfileall))

    dsegyfiles, dsegyfileall = statsamefilesbytype(d, suffix="segy")

    f.writeLine("Duplicate segy files : " + str(dsegyfiles) + " All Duplicate file counts : " + str(dsegyfileall))

    outduplicatefilemsgbytype(d, f, suffix="sgy")
    outduplicatefilemsgbytype(d, f, suffix="segy")

    outduplicatefilemsg(d, f)

    outonefilemsg(d, f)

    f.close()







    #print("file_name : ", file_path + "/" + file_name)

if __name__ == '__main__':

    filesdict.clear()

    print("filesdict id ", id(filesdict))



    #statpathfileattr("/home/hadoop/dataMoveCode/pathlist.txt", "/home/hadoop/dataMoveCode")
    statpathfileattr("E:/1.txt")

    #d = buildfilenamedictformfilelist("/home/hadoop/tmp/dir-and-files.list")
    """存储目的路径根目录"""
    """
    destrootpath = "/home/hadoop/tmp4"
    path = "E:/data"
    d =  scansamenamefile(path)


    print(d)

    for k in d:
        print("key =", k)
        print("files = ", len(d[k]))

    print("allfiles", statallfiles(d))
    print("sameallfiles", statsamenamefiles(d))

    f = CoSailTextFile("e:/sss.log")

    f.open("w")

    f.writeLine("stat path : " + path)
    f.writeLine("all files : " + str(statallfiles(d)))
    f.writeLine("Duplicate files : " +str(statsamenamefiles(d)))
    outduplicatefilemsg(d, f)

    outonefilemsg(d, f)

    f.close()

    f = CoSailTextFile("E:/1.txt")
    f.open("r")
    text = f.readLines()

    print((text))
    """

